package com.beerq.view;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.beerq.BuildConfig;
import com.beerq.R;
import com.beerq.util.BeerQPref;
import com.beerq.util.Constants;

public class MainActivity extends BaseActivity {


    private FirstFragment mFirstFragment;
    private SecondFragment mSecondFragment;
    private ThiridFragment mThiridFragment;
    private ForthFragment mForthFragment;

    /**
     * 底部四个按钮
     */
    private LinearLayout mTabFirst;
    private LinearLayout mTabSecond;
    private LinearLayout mTabThirid;
    private LinearLayout mTabForth;

    private ImageView mTabImg1;
    private ImageView mTabImg2;
    private ImageView mTabImg3;
    private ImageView mTabImg4;

    private TextView mTxtTab1;
    private TextView mTxtTab2;
    private TextView mTxtTab3;
    private TextView mTxtTab4;


    private TextView mTxtTime;


    private  static final int MSG_START_TIMEOUT = 1;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        statusBarType=0;
        requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  //设置全屏
        super.onCreate(savedInstanceState);

        setMainContentView(R.layout.activity_main);

        hideTopBar();

        initViews();

//        WebView webView=(WebView)findViewById(R.id.webview_test);
//        webView.loadUrl(Constants.domain+"#/list");
//        Constants.firstWebView=webView;
//        if(!BeerQPref.getInstance(this).isShowAlarm()){
//            Intent intent=new Intent(this,AlarmActivity.class);
//            startActivityForResult(intent,0);
//        }

        Message message = handler.obtainMessage(MSG_START_TIMEOUT);
        handler.sendMessageDelayed(message, 1000);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setTabSelection(0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==0 && resultCode==1){
            finish();
        }

    }

    private long mkeyTime;


    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if ((System.currentTimeMillis() - mkeyTime) > 1500) {
            mkeyTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出啤气", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }

    private void initViews(){

        mTxtTime=(TextView)findViewById(R.id.txt_time);

        mTabFirst = (LinearLayout) findViewById(R.id.linear_tab_1);
        mTabSecond = (LinearLayout) findViewById(R.id.linear_tab_2);
        mTabThirid = (LinearLayout) findViewById(R.id.linear_tab_3);
        mTabForth = (LinearLayout) findViewById(R.id.linear_tab_4);

        mTabImg1= (ImageView) findViewById(R.id.tab_img_1);
        mTabImg2= (ImageView) findViewById(R.id.tab_img_2);
        mTabImg3= (ImageView) findViewById(R.id.tab_img_3);
        mTabImg4= (ImageView) findViewById(R.id.tab_img_4);

        mTxtTab1= (TextView) findViewById(R.id.tab_txt_1);
        mTxtTab2= (TextView) findViewById(R.id.tab_txt_2);
        mTxtTab3= (TextView) findViewById(R.id.tab_txt_3);
        mTxtTab4= (TextView) findViewById(R.id.tab_txt_4);

//
        mTabFirst.setOnClickListener(this);
        mTabSecond.setOnClickListener(this);
        mTabThirid.setOnClickListener(this);
        mTabForth.setOnClickListener(this);

        setTabSelection(2);
    }



    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.linear_tab_1:
                setTabSelection(0);
                break;
            case R.id.linear_tab_2:
                setTabSelection(1);
                break;
            case R.id.linear_tab_3:
                setTabSelection(2);
                break;
            case R.id.linear_tab_4:
                setTabSelection(3);
                break;
            default:
                break;
        }
    }

    private void setTabSelection(int index){
        // 重置按钮
        resetBtn();
        // 开启一个Fragment事务
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index){
            case 0:

//                mView_back_to_top1.setVisibility(View.GONE);
//                if(isShowFirstBackToTop){
//                    mView_back_to_top.setVisibility(View.VISIBLE);
//                }

                setBarTitle("啤酒商店");
                mTxtTab1.setTextColor(0xFFFFFFFF);
                mTabImg1.setImageDrawable(getResources().getDrawable(R.drawable.good_beer_1) );
                // 当点击了消息tab时，改变控件的图片和文字颜色
//                mTabFirst.setBackgroundResource(R.color.main_color);
//                ((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin)).setImageResource(R.drawable.tab_weixin_pressed);
                if (mFirstFragment == null){
                    mFirstFragment = new FirstFragment();
                    transaction.add(R.id.fragment_content, mFirstFragment);
                } else{
                    transaction.show(mFirstFragment);
                }
                break;
            case 1:
                setBarTitle("啤酒志");
//                mView_back_to_top.setVisibility(View.GONE);
//
//                if(isShowSecondBackToTop){
//                    mView_back_to_top1.setVisibility(View.VISIBLE);
//                }

                mTxtTab2.setTextColor(0xFFFFFFFF);
                // 当点击了消息tab时，改变控件的图片和文字颜色
//                mTabSecond.setBackgroundResource(R.color.main_color);
                mTabImg2.setImageDrawable(getResources().getDrawable(R.drawable.blog_1) );
                if (mSecondFragment == null){
                    mSecondFragment = new SecondFragment();
                    transaction.add(R.id.fragment_content, mSecondFragment);
                } else{
                    transaction.show(mSecondFragment);
                }
                break;
            case 2:
                setBarTitle("发啤气");
//                mView_back_to_top1.setVisibility(View.INVISIBLE);
//                mView_back_to_top.setVisibility(View.INVISIBLE);
                mTxtTab3.setTextColor(0xFFFFFFFF);
                // 当点击了动态tab时，改变控件的图片和文字颜色
//                mTabThirid.setBackgroundResource(R.color.main_color);
                mTabImg3.setImageDrawable(getResources().getDrawable(R.drawable.post_blog_1) );
                if (mThiridFragment == null){
                    mThiridFragment = new ThiridFragment();
                    transaction.add(R.id.fragment_content, mThiridFragment);
                } else{
                    transaction.show(mThiridFragment);
                }
                break;
            case 3:
                setBarTitle("自个儿");
//                mView_back_to_top1.setVisibility(View.INVISIBLE);
//                mView_back_to_top.setVisibility(View.INVISIBLE);
                mTxtTab4.setTextColor(0xFFFFFFFF);
                // 当点击了设置tab时，改变控件的图片和文字颜色
//                mTabForth.setBackgroundResource(R.color.main_color);
                mTabImg4.setImageDrawable(getResources().getDrawable(R.drawable.me_1));
                if (mForthFragment == null){
                    mForthFragment = new ForthFragment();
                    transaction.add(R.id.fragment_content, mForthFragment);
                } else{
                    transaction.show(mForthFragment);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void resetBtn(){
        mTabImg1.setImageDrawable(getResources().getDrawable(R.drawable.good_beer) );
        mTabImg2.setImageDrawable(getResources().getDrawable(R.drawable.blog) );
        mTabImg3.setImageDrawable(getResources().getDrawable(R.drawable.post_blog) );
        mTabImg4.setImageDrawable(getResources().getDrawable(R.drawable.me) );

        mTxtTab1.setTextColor(0xFFaaaaaa);
        mTxtTab2.setTextColor(0xFFaaaaaa);
        mTxtTab3.setTextColor(0xFFaaaaaa);
        mTxtTab4.setTextColor(0xFFaaaaaa);

//        mTabFirst.setBackgroundColor(0xffcccccc);
//        mTabSecond.setBackgroundColor(0xffcccccc);
//        mTabThirid.setBackgroundColor(0xffcccccc);
//        mTabForth.setBackgroundColor(0xffcccccc);
//        ((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin)).setImageResource(R.drawable.tab_weixin_normal);
//        ((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend)).setImageResource(R.drawable.tab_find_frd_normal);
//        ((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact)).setImageResource(R.drawable.tab_address_normal);
//        ((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting)).setImageResource(R.drawable.tab_settings_normal);

    }

    private void hideFragments(FragmentTransaction transaction){
        if (mFirstFragment != null){
            transaction.hide(mFirstFragment);
        }
        if (mSecondFragment != null){
            transaction.hide(mSecondFragment);
        }
        if (mThiridFragment != null){
            transaction.hide(mThiridFragment);
        }
        if (mForthFragment != null){
            transaction.hide(mForthFragment);
        }

    }

    private int mSecond= BuildConfig.DEBUG?1:3;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_START_TIMEOUT:
                    mSecond--;
                    mTxtTime.setText(mSecond + "s");

                    if (mSecond > 0) {
                        Message message = handler.obtainMessage(MSG_START_TIMEOUT);
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        findViewById(R.id.rel_splash).setVisibility(View.GONE);
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
                        getWindow().setAttributes(lp);
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                    }
            }
            super.handleMessage(msg);
        }
    };

}
